<?php
namespace Operationtest\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;

class IndexController extends AbstractActionController {    
    protected $marcheTable;
    protected $storeTable;
    protected $rhdataTable;
    protected $operationTable;
    
    public function setEventManager(EventManagerInterface $events) {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/operationtest');
        }, 100);
 
        return $this;
    }
    
    public function getMarcheTable() {
        if (!$this->marcheTable) {
            $sm = $this->getServiceLocator();
            $this->marcheTable = $sm->get('Operationtest\Model\MarcheTable');
        }
        return $this->marcheTable;
    }
    
    public function getStoreTable() {
        if (!$this->storeTable) {
            $sm = $this->getServiceLocator();
            $this->storeTable = $sm->get('Operationtest\Model\StoreTable');
        }
        return $this->storeTable;
    }
    
    public function getRhdataTable() {
        if (!$this->rhdataTable) {
            $sm = $this->getServiceLocator();
            $this->rhdataTable = $sm->get('Operationtest\Model\RhdataTable');
        }
        return $this->rhdataTable;
    }
    
    public function getOperationTable() {
        if(!$this->operationTable) {
            $sm = $this->getServiceLocator();
            $this->operationTable = $sm->get('RHX\Model\Table\OperationTable');
        }
        return $this->operationTable;
    }
    
    public function indexAction() {        
        $dataCritere = new Container('dataCritere');
        
        return new ViewModel(array(
            'marches' => $this->getMarcheTable()->listAll(),
            'stores' => $this->getStoreTable()->fetchAll(),
            'periodY' => $dataCritere->offsetGet('periodY'),
            'periodM' => $dataCritere->offsetGet('periodM'),
            'semaine' =>$dataCritere->offsetGet('semaine'),
            'specific_date' => $dataCritere->offsetGet('specific_date'),
        ));        
    }
    
    public function rhdataAction() {
        $dStart = date('Y-m-01');
        $now = date('d');
        $yesterday = $now - 1;
        $dEnd = date('Y-m-'.$yesterday);
        $thisMonth = $this->getDatesBetween($dStart, $dEnd);
        $stores = $this->getStoreTable()->fetchAllForRHData();
        
        foreach($stores as $store) {
            foreach($thisMonth as $year) {
                foreach($year as $month) {
                    foreach($month as $day) {
                        $rhSiteId = $store->groupmentCode;
                        if($store->groupmentCode == 999) $rhSiteId = 0;
                        
                        $oldMo = $this->getRhdataTable()->getOldMO($store->idStore, $day);
                        $mo = $this->getOperationTable()->getMO($rhSiteId, $day);
                        
                        if(!$oldMo) {                            
                            $this->getRhdataTable()->addMO($day, $store->idStore, $mo);
                        } else {
                            $this->getRhdataTable()->updateMO($day, $store->idStore, $mo);
                        }
                    }
                }
            }
        }
        
        return new ViewModel(array(
            'thisMonth' => $thisMonth,
        ));
    }
    
    public function getDatesBetween($dStart, $dEnd) {
        $iStart = strtotime ($dStart);
        $iEnd = strtotime ($dEnd);
        if (false === $iStart || false === $iEnd) {
            return false;
        }
        $aStart = explode ('-', $dStart);
        $aEnd = explode ('-', $dEnd);
        if (count ($aStart) !== 3 || count ($aEnd) !== 3) {
            return false;
        }
        if (false === checkdate ($aStart[1], $aStart[2], $aStart[0]) || false === checkdate ($aEnd[1], $aEnd[2], $aEnd[0]) || $iEnd <= $iStart) {
            return false;
        }
        for ($i = $iStart; $i < $iEnd + 86400; $i = strtotime ('+1 day', $i) ) {
            $sDateToArr = strftime ('%Y-%m-%d', $i);
            $sYear = substr ($sDateToArr, 0, 4);
            $sMonth = substr ($sDateToArr, 5, 2);
            $aDates[$sYear][$sMonth][] = $sDateToArr;
        }
        if (isset ($aDates) && !empty ($aDates)) {
            return $aDates;
        } else {
            return false;
        }
    }
}